package com.yanfan.zutai.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.zutai.domain.BDeviceHistory;
import com.yanfan.zutai.domain.BDeviceRealData;
import com.yanfan.zutai.mapper.BDeviceHistoryMapper;
import com.yanfan.zutai.service.IBDeviceHistoryService;
import com.yanfan.zutai.service.IBDeviceManagerService;
import com.yanfan.zutai.service.IBDeviceRealDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 设备历史记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-02-27
 */
@Service
public class BDeviceHistoryServiceImpl implements IBDeviceHistoryService {
    @Autowired
    private BDeviceHistoryMapper bDeviceHistoryMapper;
    @Autowired
    private IBDeviceManagerService bDeviceManagerService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private IBDeviceRealDataService bDeviceRealDataService;

    /**
     * 查询设备历史记录
     *
     * @param id 设备历史记录ID
     * @return 设备历史记录
     */
    @Override
    public BDeviceHistory selectBDeviceHistoryById(Long id) {
        return bDeviceHistoryMapper.selectBDeviceHistoryById(id);
    }

    /**
     * 查询设备历史记录列表
     *
     * @param bDeviceHistory 设备历史记录
     * @return 设备历史记录
     */
    @Override
    public List<BDeviceHistory> selectBDeviceHistoryList(BDeviceHistory bDeviceHistory) {
        return bDeviceHistoryMapper.selectBDeviceHistoryList(bDeviceHistory);
    }

    /**
     * 新增设备历史记录
     *
     * @param bDeviceHistory 设备历史记录
     * @return 结果
     */
    @Override
    public int insertBDeviceHistory(BDeviceHistory bDeviceHistory) {
        Snowflake snowflake = IdUtil.getSnowflake(6, 2);
        long id = snowflake.nextId();
        bDeviceHistory.setId(id);
        return bDeviceHistoryMapper.insertBDeviceHistory(bDeviceHistory);
    }

    /**
     * 修改设备历史记录
     *
     * @param bDeviceHistory 设备历史记录
     * @return 结果
     */
    @Override
    public int updateBDeviceHistory(BDeviceHistory bDeviceHistory) {
        bDeviceHistory.setUpdateTime(DateUtils.getNowDate());
        return bDeviceHistoryMapper.updateBDeviceHistory(bDeviceHistory);
    }

    /**
     * 批量删除设备历史记录
     *
     * @param ids 需要删除的设备历史记录ID
     * @return 结果
     */
    @Override
    public int deleteBDeviceHistoryByIds(Long[] ids) {
        return bDeviceHistoryMapper.deleteBDeviceHistoryByIds(ids);
    }

    /**
     * 删除设备历史记录信息
     *
     * @param id 设备历史记录ID
     * @return 结果
     */
    @Override
    public int deleteBDeviceHistoryById(Long id) {
        return bDeviceHistoryMapper.deleteBDeviceHistoryById(id);
    }

    @Override
    public BDeviceHistory getParamList(BDeviceHistory bDeviceHistory) {
        Map map = JSON.parseObject(bDeviceHistory.getDeviceData(), Map.class);
        BDeviceRealData bDeviceRealData = new BDeviceRealData();
        bDeviceRealData.setDeviceImei(bDeviceHistory.getDeviceImei());
        List<BDeviceRealData> bDeviceRealDatas = bDeviceRealDataService.selectBDeviceRealDataList(bDeviceRealData);
        for (int i = 0; i < bDeviceRealDatas.size(); i++) {
            bDeviceRealData = bDeviceRealDatas.get(i);
            Object paramValue = map.get(bDeviceRealData.getParamField());
            if (paramValue != null) {
                bDeviceRealData.setParamValue(paramValue.toString());
            }
        }
        bDeviceHistory.setBDeviceRealDataList(bDeviceRealDatas);
        return bDeviceHistory;
    }
}
